﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class OppOrderRuleDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOppOrderRuleDetailDA oppOrderRuleDetailDA = DataAccess.CreateOppOrderRuleDetailDA();
        private PubParasInfo pubParasInfo;

        public OppOrderRuleDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public OppOrderRuleDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public OppOrderRuleDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> oppOrderRuleIDs, List<string> LineIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<OppOrderRuleDetailInfo> oppOrderRuleDetailInfos = new List<OppOrderRuleDetailInfo>();
                OppOrderRuleDetailInfo oppOrderRuleDetailInfo = null;
                for (i = 0; i < oppOrderRuleIDs.Count; i++)
                {
                    oppOrderRuleDetailInfo = oppOrderRuleDetailDA.GetOppOrderRuleDetailByID(dataBase, tran, oppOrderRuleIDs[i], LineIDs[i], this.dataBaseID[0]);
                    oppOrderRuleDetailInfos.Add(oppOrderRuleDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (oppOrderRuleDetailDA.DeleteOppOrderRuleDetail(dataBase, tran, oppOrderRuleIDs, LineIDs, this.dataBaseID) > 0)
                {
                    for (i = 0; i < oppOrderRuleIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<OppOrderRuleDetailInfo>(dataBase, tran, oppOrderRuleDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "OPP_ORDER_RULE_DETAIL", this.dataBaseID);
                        temp = temp + oppOrderRuleIDs[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllDetailInfo()
        {
            return oppOrderRuleDetailDA.GetAllOppOrderRuleDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(OppOrderRuleDetailQueryEntity oppOrderRuleDetailQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.OppOrderRuleID))
            {
                sqlWhere.Add(" OPP_ORDER_RULE_ID " + oppOrderRuleDetailQuery.OppOrderRuleID);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.LineID))
            {
                sqlWhere.Add(" LINE_ID " + oppOrderRuleDetailQuery.LineID);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.OppOrderStrategy))
            {
                sqlWhere.Add(" OPP_ORDER_STRATEGY " + oppOrderRuleDetailQuery.OppOrderStrategy);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.PriorityFrom))
            {
                sqlWhere.Add(" PRIORITY_FROM " + oppOrderRuleDetailQuery.PriorityFrom);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.PriorityTo))
            {
                sqlWhere.Add(" PRIORITY_TO " + oppOrderRuleDetailQuery.PriorityTo);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.OrderDateStartDays))
            {
                sqlWhere.Add(" ORDER_DATE_START_DAYS " + oppOrderRuleDetailQuery.OrderDateStartDays);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.OrderDateEndDays))
            {
                sqlWhere.Add(" ORDER_DATE_END_DAYS " + oppOrderRuleDetailQuery.OrderDateEndDays);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.ReqShipDateStartDays))
            {
                sqlWhere.Add(" REQ_SHIP_DATE_START_DAYS " + oppOrderRuleDetailQuery.ReqShipDateStartDays);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.ReqShipDateEndDays))
            {
                sqlWhere.Add(" REQ_SHIP_DATE_END_DAYS " + oppOrderRuleDetailQuery.ReqShipDateEndDays);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.OrderTypeInclude))
            {
                sqlWhere.Add(" ORDER_TYPE_INCLUDE " + oppOrderRuleDetailQuery.OrderTypeInclude);
            }
            if (!string.IsNullOrEmpty(oppOrderRuleDetailQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + oppOrderRuleDetailQuery.IsActive);
            }
            return oppOrderRuleDetailDA.GetOppOrderRuleDetailByQueryList(sqlWhere, oppOrderRuleDetailQuery, this.dataBaseID[0]);
        }

        public OppOrderRuleDetailInfo GetDetailInfoByID(string mainID, string lineID)
        {
            return oppOrderRuleDetailDA.GetOppOrderRuleDetailByID(mainID, lineID, this.dataBaseID[0]);
        }

        public List<OppOrderRuleDetailInfo> GetDetailInfoListByID(string mainID)
        {
            return oppOrderRuleDetailDA.GetOppOrderRuleDetailListByID(mainID, this.dataBaseID[0]);
        }

        public string InsertDetail(object obj)
        {
            string oppOrderRuleID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                OppOrderRuleDetailInfo oppOrderRuleDetailInfo = obj as OppOrderRuleDetailInfo;
                if (oppOrderRuleDetailDA.CheckOppOrderRuleDetailIDUnique(dataBase, tran, oppOrderRuleDetailInfo.OppOrderRuleID, oppOrderRuleDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    throw new Exception("OppOrderRuleID:" + oppOrderRuleDetailInfo.OppOrderRuleID + ",LineID:" + oppOrderRuleDetailInfo.LineID.ToString() + ",已经存在,请重新输入!");
                }
                oppOrderRuleDetailInfo.UpdatedBy = oppOrderRuleDetailInfo.CreatedBy;
                oppOrderRuleDetailInfo.CreatedDate = DateTime.Now;
                oppOrderRuleDetailInfo.UpdatedDate = DateTime.Now;
                if (oppOrderRuleDetailDA.InsertOppOrderRuleDetail(dataBase, tran, oppOrderRuleDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OppOrderRuleDetailInfo>(dataBase, tran, null, oppOrderRuleDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "OPP_ORDER_RULE_DETAIL", this.dataBaseID[0]);
                    oppOrderRuleID = oppOrderRuleDetailInfo.OppOrderRuleID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return oppOrderRuleID;
        }

        public string UpdateDetail(object obj)
        {
            string oppOrderRuleID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                OppOrderRuleDetailInfo oppOrderRuleDetailInfo = obj as OppOrderRuleDetailInfo;
                oppOrderRuleDetailInfo.UpdatedDate = DateTime.Now;
                OppOrderRuleDetailInfo oldoppOrderRuleDetailInfo = oppOrderRuleDetailDA.GetOppOrderRuleDetailByID(dataBase, tran, oppOrderRuleDetailInfo.OppOrderRuleID, oppOrderRuleDetailInfo.LineID.ToString(), this.dataBaseID[0]);
                if (oppOrderRuleDetailDA.UpdateOppOrderRuleDetail(oppOrderRuleDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OppOrderRuleDetailInfo>(oldoppOrderRuleDetailInfo, oppOrderRuleDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "OPP_ORDER_RULE_DETAIL", this.dataBaseID[0]);
                    oppOrderRuleID = oldoppOrderRuleDetailInfo.OppOrderRuleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return oppOrderRuleID;
        }
    }
}

